Page({
    data: {
      WorkerName: '',
      WorkerPhone: '',
      WorkerAccount: '',
      WorkerPassword: ''
    },
    inputWorkerName: function (e) {
      this.setData({
        WorkerName: e.detail.value
      });
    },
    inputWorkerPhone: function (e) {
      this.setData({
        WorkerPhone: e.detail.value
      });
    },
    inputWorkerAccount: function (e) {
      this.setData({
      WorkerAccount: e.detail.value
      });
    },
    inputWorkerPassword: function (e) {
      this.setData({
        WorkerPassword: e.detail.value
      });
    },
    workerRegister: function () {
      let url = 'http://8.140.225.174:5000/worker/register';
      let data = {
        WorkerName: this.data.WorkerName,
        WorkerPhone: this.data.WorkerPhone,
        WorkerAccount: this.data.WorkerAccount,
        WorkerPassword: this.data.WorkerPassword
      };
      this.sendWorkerRegisterRequest(url, data).then((resData) => {
        if (resData.code === 200) {
          wx.showToast({
            title: '注册成功',
            icon: 'success'
          });
          wx.redirectTo({
            url: '/pages/login/login'
          });
        } else {
          wx.showToast({
            title: '注册失败',
            icon: 'none'
          });
        }
      }).catch((error) => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
          });
      });
    },
    sendWorkerRegisterRequest: function (url, data) {
      return new Promise((resolve, reject) => {
        wx.request({
          url: url,
          method: 'POST',
          data: data,
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.code == 200) {
              resolve(res.data);
            } else {
              reject(res.data);
          }
          },
          fail: (err) => {
            reject(err);
          }
        });
      });
    }
  })